# 在这里写上你的代码 :-)
# -*- coding:utf-8 -*-
# 导入相关模块
from machine import Pin,PWM
from xlazydogUnpack3 import BLEpack
import bluetooth,ble_uart_peripheral,utime

class BluetoothCar:
    # 蓝牙模式，通过手机蓝牙连接机器人进行控制
    def __init__(self):
        # 配置蓝牙信息并开启蓝牙通信
        self.ble = bluetooth.BLE()
        self.uart = ble_uart_peripheral.BLEUART(self.ble,name='LaLaCar')
        self.PWMCar = PWMCarControl()
        self.ble_control()  # 开启蓝牙控制

    def ble_control(self):
        # 用于接收手机控制命令
        blepack = BLEpack(0,2,0,0,0)
        Pin(2, Pin.OUT).value(0)  # 初始化蓝牙指示灯
        while True:
            if self.uart.any():  # 查询是否有信息
                self.text = blepack.get_byte(self.uart.read(128))# 默认单次最多接收128字节
                try:
                    self.PWMCar.CarRun(self.text)
                except:
                    pass
                
class PWMCarControl():
    def __init__(self):
        #LeftPin
        self.L1 = PWM(Pin(32), freq=1000, duty=0)
        self.L2 = PWM(Pin(33), freq=1000, duty=0)
        self.L3 = PWM(Pin(25), freq=1000, duty=0)
        self.L4 = PWM(Pin(26), freq=1000, duty=0)
        #RightPin
        self.R1 = PWM(Pin(17), freq=1000, duty=0)
        self.R2 = PWM(Pin(16), freq=1000, duty=0)
        self.R3 = PWM(Pin(4), freq=1000, duty=0)
        self.R4 = PWM(Pin(0), freq=1000, duty=0)
        self.speed = 700
        self.Stop()

    def Go(self):
        self.L1.duty(0)
        self.L2.duty(self.speed)
        self.L3.duty(self.speed)
        self.L4.duty(0)
        self.R1.duty(0)
        self.R2.duty(self.speed)
        self.R3.duty(self.speed)
        self.R4.duty(0)
    def Back(self):
        self.L1.duty(self.speed)
        self.L2.duty(0)
        self.L3.duty(0)
        self.L4.duty(self.speed)
        self.R1.duty(self.speed)
        self.R2.duty(0)
        self.R3.duty(0)
        self.R4.duty(self.speed)
    def Left(self):
        self.L1.duty(self.speed)
        self.L2.duty(0)
        self.L3.duty(0)
        self.L4.duty(self.speed)
        self.R1.duty(0)
        self.R2.duty(self.speed)
        self.R3.duty(self.speed)
        self.R4.duty(0)
    def Right(self):
        self.L1.duty(0)
        self.L2.duty(self.speed)
        self.L3.duty(self.speed)
        self.L4.duty(0)
        self.R1.duty(self.speed)
        self.R2.duty(0)
        self.R3.duty(0)
        self.R4.duty(self.speed)
    def Stop(self):
        self.L1.duty(0)
        self.L2.duty(0)
        self.L3.duty(0)
        self.L4.duty(0)
        self.R1.duty(0)
        self.R2.duty(0)
        self.R3.duty(0)
        self.R4.duty(0)

    def CarRun(self,text):
        command=text[0]
        mod=text[1]
        if mod == 'p':
            self.speed = 650
        if command == 'c':  # 停止命令
            self.Stop()
            Pin(2, Pin.OUT).value(0)
            self.speed = 650
        elif command == 'f':  # 前进命令
            self.Go()
            Pin(2, Pin.OUT).value(1)
            self.speed = self.speed + 10
        elif command == 'b':  # 后退命令
            self.Back()
            Pin(2, Pin.OUT).value(1)
            self.speed = self.speed + 10
        elif command == 'l':  # 左转命令
            self.Left()
            Pin(2, Pin.OUT).value(1)
            self.speed = self.speed + 10
        elif command == 'r':  # 右转命令
            self.Right()
            Pin(2, Pin.OUT).value(1)
            self.speed = self.speed + 10
        elif command == 's':  # 右转命令
            self.speed = 1000 
            self.Back()
            utime.sleep_ms(100)
            self.Left()
            utime.sleep_ms(300)
            self.Back()
            utime.sleep_ms(100)
            Pin(2, Pin.OUT).value(1)
            
        else:
            return False

Car = BluetoothCar()